package org.springblade.modules.admin.service.suger.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.admin.dto.plan.PlanContentExcelSearchDTO;
import org.springblade.modules.admin.dto.sugar.SugarSearchDTO;
import org.springblade.modules.admin.entity.UserSignDetail;
import org.springblade.modules.admin.service.IBuildingContractPlusService;
import org.springblade.modules.admin.service.UserSignDetailService;
import org.springblade.modules.admin.service.suger.SugerSignAndContractService;
import org.springblade.modules.admin.vo.sugar.SugerContractVo;
import org.springblade.modules.admin.vo.sugar.SugerSignDetailVo;
import org.springblade.modules.system.entity.User;
import org.springblade.modules.system.service.IDeptService;
import org.springblade.modules.system.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zcc
 * @version 1.0
 * @date 2022/10/11 9:58
 */
@Service
public class SugerSignAndContractServiceImpl implements SugerSignAndContractService {


	@Autowired
	private UserSignDetailService userSignDetailService;

	@Autowired
	private IBuildingContractPlusService buildingContractPlusService;
	@Autowired
	private IDeptService deptService;
	@Autowired
	private IUserService userService;

	@Override
	public SugerSignDetailVo getSugerSignDetail(SugarSearchDTO searchDTO) {

		List<Long> deptIds = deptService.selectDeptAndAllChildIds(searchDTO.getDeptIds());
		List<User> users = userService.list();
		List<Long> userIds = new ArrayList<>();
		deptIds.forEach(e -> {
			userIds.addAll(users.stream().filter(u -> Func.isNotEmpty(u.getDeptId()) && u.getDeptId().contains(String.valueOf(e))).map(User::getId).collect(Collectors.toList()));
		});

		SugerSignDetailVo sugerSignDetailVo = new SugerSignDetailVo();
		if(Func.isNotEmpty(userIds)){
			Date date = new Date();
			long today = Convert.toLong(DateUtil.format(date, "yyyyMMdd"));
			PlanContentExcelSearchDTO planContentExcelSearchDTO = new PlanContentExcelSearchDTO();
			planContentExcelSearchDTO.setEndDate(date);
			planContentExcelSearchDTO.setStartDate(DateUtil.beginOfMonth(date));
			planContentExcelSearchDTO.setUserIds(userIds.stream().distinct().collect(Collectors.toList()));
			List<UserSignDetail> signDetails = userSignDetailService.getList(planContentExcelSearchDTO);

			//处理当天
			final List<UserSignDetail> todayDetailList = signDetails.stream().filter(o -> o.getSignDate() == today).collect(Collectors.toList());
			if(Func.isNotEmpty(todayDetailList)){
				final Integer holiday = todayDetailList.get(0).getHoliday();
				sugerSignDetailVo.setHoliday(holiday);
				if (holiday == 0) {
					sugerSignDetailVo.setPlanUserCount(todayDetailList.size());

					int workUserCount = todayDetailList.stream().filter(o -> o.getSignTime() != null).collect(Collectors.toList()).size();
					sugerSignDetailVo.setWorkUserCount(workUserCount);

					int beLateUserCount = todayDetailList.stream().filter(o -> Convert.toInt(o.getSignBeLate(), -1) == 0).collect(Collectors.toList()).size();
					sugerSignDetailVo.setBeLateUserCount(beLateUserCount);

					int locateAbnormalUserCount1 = todayDetailList.stream().filter(o -> Convert.toInt(o.getSignType(), -1) == 2).collect(Collectors.toList()).size();
					int locateAbnormalUserCount2 = todayDetailList.stream().filter(o -> Convert.toInt(o.getSignOutType(), -1) == 2).collect(Collectors.toList()).size();
					sugerSignDetailVo.setLocateAbnormalUserCount(locateAbnormalUserCount1 + locateAbnormalUserCount2);

					int leaveEarlyUserCount = todayDetailList.stream().filter(o -> Convert.toInt(o.getSignOutLeaveEarly(), -1) == 0).collect(Collectors.toList()).size();
					sugerSignDetailVo.setLeaveEarlyUserCount(leaveEarlyUserCount);

					int newSignUserCounts1 = todayDetailList.stream().filter(o -> Convert.toInt(o.getNewSign(), -1) == 1).collect(Collectors.toList()).size();
					int newSignUserCounts2 = todayDetailList.stream().filter(o -> Convert.toInt(o.getNewSignOut(), -1) == 1).collect(Collectors.toList()).size();
					sugerSignDetailVo.setNewSignUserCounts(newSignUserCounts1 + newSignUserCounts2);

				} else {
					int overtimeUserCounts = todayDetailList.stream().filter(o -> o.getSignOutTime() != null).collect(Collectors.toList()).size();
					sugerSignDetailVo.setOvertimeUserCounts(overtimeUserCounts);
				}
			}


			//处理本月
			int monthBeLateUserCount = signDetails.stream().filter(o -> o.getHoliday() == 0 && Convert.toInt(o.getSignBeLate(), -1) == 0).collect(Collectors.toList()).size();
			sugerSignDetailVo.setMonthBeLateUserCount(monthBeLateUserCount);

			int monthNewSignUserCounts1 = signDetails.stream().filter(o -> o.getHoliday() == 0 && Convert.toInt(o.getNewSign(), -1) == 1).collect(Collectors.toList()).size();
			int monthNewSignUserCounts2 = signDetails.stream().filter(o -> o.getHoliday() == 0 && Convert.toInt(o.getNewSignOut(), -1) == 1).collect(Collectors.toList()).size();
			sugerSignDetailVo.setMonthNewSignUserCounts(monthNewSignUserCounts1 + monthNewSignUserCounts2);

			int monthLeaveEarlyUserCount = signDetails.stream().filter(o -> o.getHoliday() == 0 && Convert.toInt(o.getSignOutLeaveEarly(), -1) == 0).collect(Collectors.toList()).size();
			sugerSignDetailVo.setMonthLeaveEarlyUserCount(monthLeaveEarlyUserCount);

			int monthNoSignCounts1 = signDetails.stream().filter(o -> o.getSignDate() != today && o.getHoliday() == 0 && o.getSignTime() == null).collect(Collectors.toList()).size();
			int monthNoSignCounts2 = signDetails.stream().filter(o -> o.getSignDate() != today && o.getHoliday() == 0 && o.getSignOutTime() == null).collect(Collectors.toList()).size();
			sugerSignDetailVo.setMonthNoSignCounts(monthNoSignCounts1 + monthNoSignCounts2);

			int monthLocateAbnormalUserCount1 = signDetails.stream().filter(o -> o.getHoliday() == 0 && Convert.toInt(o.getSignType(), -1) == 2).collect(Collectors.toList()).size();
			int monthLocateAbnormalUserCount2 = signDetails.stream().filter(o -> o.getHoliday() == 0 && Convert.toInt(o.getSignOutType(), -1) == 2).collect(Collectors.toList()).size();
			sugerSignDetailVo.setMonthLocateAbnormalUserCount(monthLocateAbnormalUserCount1 + monthLocateAbnormalUserCount2);

			int monthOvertimeUserCounts = signDetails.stream().filter(o -> o.getHoliday() == 1 && (o.getSignTime() != null && o.getSignOutTime() != null)).collect(Collectors.toList()).size();
			sugerSignDetailVo.setMonthOvertimeUserCounts(monthOvertimeUserCounts);
		}

		return sugerSignDetailVo;
	}

	@Override
	public List<SugerContractVo> getContractDetail() {
		return buildingContractPlusService.getSugerContractDetail();
	}
}
